//
//  MSFilterSelectCell.m
//  MSJD
//
//  Created by 箭军刘 on 2018/11/8.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSFilterSelectCell.h"


/** 筛选页选中按钮的tag */
NSInteger const kMSFilterSelectShowButtonDefaultTag = 1233333333;

@interface MSFilterSelectCell()

@property (nonatomic, strong) UILabel *nameLabel; // 提示标签
@property (nonatomic, strong) UIView *showView; // 展示选项控件

@property (nonatomic, copy) NSDictionary *contentDict;
@property (nonatomic, copy) NSArray *dictsList;
@property (nonatomic, copy) NSString *selectIdKey;


@property (nonatomic, assign) NSInteger selectButtonIndex; // 选中的按钮的序号

@end

@implementation MSFilterSelectCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initSubViews];
    }
    return self;
}

#pragma mark - Init Method

- (void)initSubViews {
    
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(15);
        make.left.offset(15);
    }];
    
    [self.contentView addSubview:self.showView];
    [self.showView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);
        make.left.offset(15);
        make.right.offset(-15);
        make.height.offset(0.0);
    }];
}


- (void)refreshFilterSelectCellWithDictionary:(NSDictionary *)dict selectId:(nonnull NSString *)selectId {
    // 判断内容是否一致
    self.contentDict = dict;
    self.selectIdKey = selectId;
    
    for (UIView *subView in self.showView.subviews) {
        [subView removeFromSuperview];
    }
    self.nameLabel.text = [MSSafeObject stringInfo:dict[@"name"]];
    
    self.dictsList = [MSSafeObject ArrayInfo:dict[@"dicts"]];
    [self addSelectButtonToShowView];
}

+ (CGFloat)caculateFilterSelectCellHeightWithDictionary:(NSDictionary *)dict {
    
    CGFloat height = 36.0 + 8.0;
    
    NSArray *dicts = [MSSafeObject ArrayInfo:dict[@"dicts"]];
    if (dict.count > 0) {
        height += (((dicts.count - 1)/4 + 1) * 44.0);
    }
    return height;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - Event Response
/** 展示的按钮点击回调 */
- (void)showViewSelectButtonClick:(UIButton *)sender {
    // 修改旧的按钮的选中装填
    NSInteger tag = sender.tag - kMSFilterSelectShowButtonDefaultTag;
    if (self.selectButtonIndex != tag) {
        UIButton *oldButton = [self.showView viewWithTag:self.selectButtonIndex + kMSFilterSelectShowButtonDefaultTag];
        if (oldButton) {
            oldButton.selected = NO;
        }
        [oldButton setBackgroundColor:rgba(0, 0, 0, 0.08)];
    }
    // 修改新按钮的选中状态
    sender.selected = !sender.selected;
    // 修改按钮的背景颜色
    [sender setBackgroundColor:sender.selected?MSThemeColorA(0.08):rgba(0, 0, 0, 0.08)];
    
    NSDictionary *selectDict = [MSSafeObject dictionaryInfo:self.dictsList[tag]];
    self.selectIdKey = selectDict[@"id"];
    self.selectButtonIndex = tag;
    if (self.ShowButtonSelectBlock) {
        self.ShowButtonSelectBlock(selectDict);
    }
}

#pragma mark - Private Method
/** 添加选中按钮 */
- (void)addSelectButtonToShowView {
    
    if (self.dictsList.count < 1) {
        self.showView.hidden = YES;
        return;
    }
    self.showView.hidden = NO;
    NSInteger dictsCount = self.dictsList.count;
    NSInteger ratio = (dictsCount < 1)?0:(((dictsCount - 1)/4)+1);
    [self.showView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(ratio * 44);
    }];
    
    NSInteger showButtonCount = 4;
    CGFloat buttonX = 0.0;
    CGFloat buttonY = 0.0;
    CGFloat buttonSpace = 10;
    CGFloat buttonWidth = (MS_SCREEN_WIDTH - 2 * 15.0 - (showButtonCount - 1) * buttonSpace) / showButtonCount;
    CGFloat buttonHeight = 36.0;
    for (NSInteger i = 0; i < dictsCount; i ++) {
        NSDictionary *filterDic = self.dictsList[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:[MSSafeObject stringInfo:filterDic[@"name"]] forState:UIControlStateNormal];
        [button setTitle:[MSSafeObject stringInfo:filterDic[@"name"]] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor colorWithHexString:@"#444444"] forState:UIControlStateNormal];
        [button setTitleColor:MSThemeColor forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setFrame:CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight)];
        [button addTarget:self action:@selector(showViewSelectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = kMSFilterSelectShowButtonDefaultTag + i;
        if (self.selectIdKey && [self.selectIdKey integerValue] == [filterDic[@"id"] integerValue]) {
            button.selected = YES;
            self.selectButtonIndex = i;
            [button setBackgroundColor:MSThemeColorA(0.08)];
        } else {
            [button setBackgroundColor:rgba(0, 0, 0, 0.08)];
        }
        [self.showView addSubview:button];
        
        if ((i + 1)%4 == 0) {
            // 换行了
            buttonX = 0.0;
            buttonY += (buttonHeight + buttonSpace);
        } else {
            buttonX += (buttonWidth + buttonSpace);
        }
    }
}

#pragma mark - Getters

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor colorWithHexString:@"#444444"];
        _nameLabel.font = [UIFont systemFontOfSize:14];
    }
    return _nameLabel;
}

- (UIView *)showView {
    if (!_showView) {
        _showView = [[UIView alloc] init];
        _showView.backgroundColor = [UIColor clearColor];
    }
    return _showView;
}

@end
